#!/usr/bin/env python
# -*- coding: utf-8 -*-

# about driver
from selenium import webdriver
from webdriver_helper import get_webdriver

# driver options
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.ie.options import Options as IeOptions
from selenium.webdriver.edge.options import Options as EdgeOptions

# driver service
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.edge.service import Service as EdgeService
from selenium.webdriver.ie.service import Service as IEService


# browser operations ------------------------------------------------------------------------------------------
def open_url(url: str, browser_type="chrome", driver_type="smart",webhub_url="", driver_path=""):
    if driver_type not in ["smart", "local", "remote"]:
        raise ValueError(f"Unsupported driver type: {driver_type}")
    if browser_type not in ["chrome", "firefox", "edge", "ie"]:
        raise ValueError(f"Unsupported browser type: {browser_type}")
    if driver_type == "smart":
        driver = __open_online_browser(browser_type)
    elif driver_type == "local":
        driver = __open_local_browser(browser_type, driver_path)
    elif driver_type == "remote":
        driver = __open_remote_browser(webhub_url, browser_type)
    else:
        raise ValueError(f"Unsupported driver type: {driver_type}")
    driver.get(url)
    return driver

def close_browser(driver):
    driver.quit()

def get_current_url(driver):
    return driver.current_url

def get_title(driver):
    return driver.title

def get_page_source(driver):
    return driver.page_source



def __open_local_browser(browser_type, driver_path):
    """
    打开指定类型的浏览器并设置选项
    :param browser_type: 浏览器类型，支持 'chrome', 'firefox', 'edge', 'ie'
    :param driver_path: 浏览器驱动文件路径
    """
    if browser_type.lower() == 'chrome':
        options = ChromeOptions()
        options.add_argument('--ignore-certificate-errors')  # 忽略不安全的SSL
        options.add_argument('--start-maximized')  # 最大化窗口
        service = ChromeService(driver_path)
        driver = webdriver.Chrome(service=service, options=options)
    elif browser_type.lower() == 'firefox':
        options = FirefoxOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--start-maximized')
        service = FirefoxService(driver_path)
        driver = webdriver.Firefox(service=service, options=options)
    elif browser_type.lower() == 'edge':
        options = EdgeOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--start-maximized')
        service = EdgeService(driver_path)
        driver = webdriver.Edge(service=service, options=options)
    elif browser_type.lower() == 'ie':
        options = IeOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--start-maximized')
        service = IEService(driver_path)
        driver = webdriver.Ie(service=service, options=options)
    else:
        raise ValueError(f"Unsupported browser type: {browser_type}")
    return driver


def __open_online_browser(browser_type="chrome"):
    if browser_type.lower() == 'chrome':
        options = ChromeOptions()
        options.add_argument('--ignore-certificate-errors')  # 忽略不安全的SSL
        options.add_argument('--start-maximized')  # 最大化窗口
        driver = get_webdriver(browser_type, options=options)
    elif browser_type.lower() == 'firefox':
        options = FirefoxOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--start-maximized')
        driver = get_webdriver(browser_type, options=options)
    elif browser_type.lower() == 'edge':
        options = EdgeOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--start-maximized')
        driver = get_webdriver(browser_type, options=options)
    elif browser_type.lower() == 'ie':
        options = IeOptions()
        options.add_argument('--ignore-certificate-errors')
        options.add_argument('--start-maximized')
        driver = get_webdriver(browser_type, options=options)
    else:
        raise ValueError(f"Unsupported browser type: {browser_type}")
    return driver


def __open_remote_browser(webhub_url: str, browser_type="chrome"):
    # check browser type
    # set capabilities
    capabilities = None
    if browser_type == "chrome":
        capabilities = webdriver.DesiredCapabilities.CHROME.copy()
        capabilities["browserName"] = "chrome"
        capabilities["version"] = ""
        capabilities["platform"] = "ANY"
        capabilities["javascriptEnabled"] = True
        capabilities["acceptInsecureCerts"] = True
    elif browser_type == "firefox":
        capabilities = {
            "browserName": "firefox",
            "version": "",
            "platform": "ANY",
            "javascriptEnabled": True,
            "acceptInsecureCerts": True
        }
    elif browser_type == "edge":
        capabilities = {
            "browserName": "edge",
            "version": "",
            "platform": "ANY",
            "javascriptEnabled": True
        }
    else:
        raise ValueError(f"Unsupported browser type: {browser_type}")
    # set headless mode
    driver = webdriver.Remote(command_executor=webhub_url, desired_capabilities=capabilities)
    return driver
